import { computed } from 'vue'
import { useMedicalRecordStore } from '@/store/modules'

export interface HealthAttribute {
  proportion: number
  description: string
  images: string[]
}

export interface AdditionalAttribute {
  name: string
  proportion: number
  description: string
  images: string[]
}

export interface HistoricalAssessment {
  date: string
  assessment: string
}

export interface HealthDemandAttributeOptimization {
  medicalAttribute: HealthAttribute
  healthAttribute: HealthAttribute
  rehabilitationAttribute: HealthAttribute
  careAttribute: HealthAttribute
  additionalAttributes: AdditionalAttribute[]
  historicalAssessments: HistoricalAssessment[]
}

export function useHealthAttributes() {
  const medicalRecordStore = useMedicalRecordStore()
  
  const formData = computed(() => {
    const storeData = medicalRecordStore.getData
    if (storeData) {
      return storeData
    }
    return medicalRecordStore.getDefaultFormData()
  })

  const updateField = (field: string, value: any) => {
    console.log("更新字段:", field, value)
    medicalRecordStore.updateField(field as any, value)
  }

  // 初始化健康需求属性优化评估数据结构
  const initHealthDemandAttributeOptimization = () => {
    if (!formData.value.healthDemandAttributeOptimization) {
      const defaultData: HealthDemandAttributeOptimization = {
        medicalAttribute: { proportion: 0, description: '', images: [] },
        healthAttribute: { proportion: 0, description: '', images: [] },
        rehabilitationAttribute: { proportion: 0, description: '', images: [] },
        careAttribute: { proportion: 0, description: '', images: [] },
        additionalAttributes: [],
        historicalAssessments: []
      }
      updateField('healthDemandAttributeOptimization', defaultData)
    }
  }

  // 更新健康属性占比
  const updateHealthAttributeProportion = (attributeType: string, value: string) => {
    const proportion = parseInt(value) || 0
    
    initHealthDemandAttributeOptimization()
    
    // 创建新的数据结构，避免直接修改响应式数据
    const currentData = { ...formData.value.healthDemandAttributeOptimization }
    if (currentData[attributeType] && typeof currentData[attributeType] === 'object' && 'proportion' in currentData[attributeType]) {
      currentData[attributeType] = {
        ...currentData[attributeType],
        proportion: proportion
      }
      updateField('healthDemandAttributeOptimization', currentData)
    }
  }

  // 更新健康属性数据（描述和图片）
  const updateHealthAttributeData = (attributeType: string, data: any) => {
    initHealthDemandAttributeOptimization()
    
    // 创建新的数据结构，避免直接修改响应式数据
    const currentData = { ...formData.value.healthDemandAttributeOptimization }
    if (currentData[attributeType] && typeof currentData[attributeType] === 'object' && 'description' in currentData[attributeType] && 'images' in currentData[attributeType]) {
      currentData[attributeType] = {
        ...currentData[attributeType],
        description: data.description || '',
        images: data.images || []
      }
      updateField('healthDemandAttributeOptimization', currentData)
    }
  }

  // 添加额外属性
  const addAdditionalAttribute = () => {
    initHealthDemandAttributeOptimization()
    
    // 创建新的数据结构，避免直接修改响应式数据
    const currentData = { ...formData.value.healthDemandAttributeOptimization }
    if (!currentData.additionalAttributes) {
      currentData.additionalAttributes = []
    }
    
    const newAttribute: AdditionalAttribute = {
      name: '',
      proportion: 0,
      description: '',
      images: []
    }
    
    currentData.additionalAttributes = [...currentData.additionalAttributes, newAttribute]
    updateField('healthDemandAttributeOptimization', currentData)
  }

  // 更新额外属性名称
  const updateAdditionalAttributeName = (index: number, value: string) => {
    if (formData.value.healthDemandAttributeOptimization?.additionalAttributes?.[index]) {
      // 创建新的数据结构，避免直接修改响应式数据
      const currentData = { ...formData.value.healthDemandAttributeOptimization }
      currentData.additionalAttributes = [...currentData.additionalAttributes]
      currentData.additionalAttributes[index] = {
        ...currentData.additionalAttributes[index],
        name: value
      }
      updateField('healthDemandAttributeOptimization', currentData)
    }
  }

  // 更新额外属性占比
  const updateAdditionalAttributeProportion = (index: number, value: string) => {
    const proportion = parseInt(value) || 0
    if (formData.value.healthDemandAttributeOptimization?.additionalAttributes?.[index]) {
      // 创建新的数据结构，避免直接修改响应式数据
      const currentData = { ...formData.value.healthDemandAttributeOptimization }
      currentData.additionalAttributes = [...currentData.additionalAttributes]
      currentData.additionalAttributes[index] = {
        ...currentData.additionalAttributes[index],
        proportion: proportion
      }
      updateField('healthDemandAttributeOptimization', currentData)
    }
  }

  // 更新额外属性数据
  const updateAdditionalAttributeData = (index: number, data: any) => {
    if (formData.value.healthDemandAttributeOptimization?.additionalAttributes?.[index]) {
      // 创建新的数据结构，避免直接修改响应式数据
      const currentData = { ...formData.value.healthDemandAttributeOptimization }
      currentData.additionalAttributes = [...currentData.additionalAttributes]
      currentData.additionalAttributes[index] = {
        ...currentData.additionalAttributes[index],
        description: data.description || '',
        images: data.images || []
      }
      updateField('healthDemandAttributeOptimization', currentData)
    }
  }

  // 删除额外属性
  const removeAdditionalAttribute = (index: number) => {
    if (formData.value.healthDemandAttributeOptimization?.additionalAttributes) {
      // 创建新的数据结构，避免直接修改响应式数据
      const currentData = { ...formData.value.healthDemandAttributeOptimization }
      currentData.additionalAttributes = currentData.additionalAttributes.filter((_, i) => i !== index)
      updateField('healthDemandAttributeOptimization', currentData)
    }
  }

  // 删除历史评估记录
  const removeHistoricalAssessment = (index: number) => {
    if (formData.value.healthDemandAttributeOptimization?.historicalAssessments) {
      // 创建新的数据结构，避免直接修改响应式数据
      const currentData = { ...formData.value.healthDemandAttributeOptimization }
      currentData.historicalAssessments = currentData.historicalAssessments.filter((_, i) => i !== index)
      updateField('healthDemandAttributeOptimization', currentData)
    }
  }

  return {
    formData,
    updateHealthAttributeProportion,
    updateHealthAttributeData,
    addAdditionalAttribute,
    updateAdditionalAttributeName,
    updateAdditionalAttributeProportion,
    updateAdditionalAttributeData,
    removeAdditionalAttribute,
    removeHistoricalAssessment
  }
}
